package com.atguigu.system.controller;


import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.result.Result;
import com.atguigu.system.Exception.GuiguException;
import com.atguigu.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll() {
        List<SysRole> roleList = sysRoleService.list();
        return Result.ok(roleList);
    }

    @ApiOperation(value = "分页获取角色的列表")
    @GetMapping("/{page}/{limit}")
    public Result index( @ApiParam(name = "page", value = "当前页码", required = true)
                             @PathVariable Long page,
                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                             @PathVariable Long limit,
                         @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                         SysRoleQueryVo roleQueryVo){
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(pageModel);

    }

    @ApiOperation(value = "获取角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole role) {
        sysRoleService.save(role);
        return Result.ok();
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysRole role) {
        sysRoleService.updateById(role);
        return Result.ok();
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "异常测试1 全局exception异常")
    @GetMapping("/Exception")
    public Result getException() throws Exception {
        try {
            int i=1/0;
        }catch (Exception e){
            e.printStackTrace();
            throw  new Exception("全局异常,触发不了就走这个异常");
        }
        return Result.ok();
    }

    @ApiOperation(value = "异常测试2 ArithmeticException 特定异常")
    @GetMapping("/ArithmeticException")
    public Result getArithmeticException() {
        try {
           int i=1/0;
        }catch (Exception e){
            e.printStackTrace();
            throw  new ArithmeticException("ArithmeticException 特定异常");
        }
        return Result.ok();
    }

    @ApiOperation(value = "异常测试3 尚硅谷异常 自定义异常")
    @GetMapping("/GuiguException")
    public Result getGuiguException() {
        try {
            int i=1/0;
        }catch (Exception e){
            e.printStackTrace();
            throw  new GuiguException(2222,"尚硅谷异常");

        }
        return Result.ok();
    }

    @ApiOperation(value = "接口测试看是否会新增")
    @GetMapping("/test")
    public Result test() {
        return Result.ok();
    }
}
